package com.futureCloudMOM.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.futureCloudMOM.common.exception.base.BaseException;
import com.futureCloudMOM.core.base.service.impl.BaseServiceImpl;
import com.futureCloudMOM.core.domain.*;
import com.futureCloudMOM.core.service.*;
import com.futureCloudMOM.core.mapper.ProduceTaskBillRoutingProcessMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 【Produce_TaskBillRoutingProcess】的数据库操作Service实现
 *
 * @author Administrator
 * @since 2023-06-06 16:03:21
 */
@Slf4j
@AllArgsConstructor
@Service
public class ProduceTaskBillRoutingProcessServiceImpl extends BaseServiceImpl<ProduceTaskBillRoutingProcessMapper, ProduceTaskBillRoutingProcess> implements ProduceTaskBillRoutingProcessService {

    // 基础工艺工序
    private final BasicRoutingProcessService basicRoutingProcessService;
    // 订单工艺用料信息
    private final ProduceTaskBillRoutingMaterialService produceTaskBillRoutingMaterialService;
    // 订单工艺加工参数
    private final ProduceTaskBillRoutingParamService produceTaskBillRoutingParamService;

    @Override
    public List<ProduceTaskBillRoutingProcess> selectListByBillId(Long billId) {
        return list(new LambdaQueryWrapper<ProduceTaskBillRoutingProcess>()
                .eq(ProduceTaskBillRoutingProcess::getTaskBillRoutingID, billId));
    }

    @Transactional
    @Override
    public void create(Long billId, Long routingId) {
        // 根据工艺路线ID 获取维护好的工艺工序信息
        List<BasicRoutingProcess> processes = basicRoutingProcessService.selectListByRoutingId(routingId);
        if (CollectionUtils.isEmpty(processes)) {
            throw new BaseException("未查询到工艺路线配置的工序");
        }
        /*
        保存订单工序
         */
        processes.forEach(basicRoutingProcess -> {
            // 复制基础数据维护好的工艺工序信息
            ProduceTaskBillRoutingProcess taskBillRoutingProcess = ProduceTaskBillRoutingProcess.getInstance(basicRoutingProcess);
            taskBillRoutingProcess.setTaskBillRoutingID(billId);
            save(taskBillRoutingProcess);
            Long taskBillRoutingProcessId = taskBillRoutingProcess.getId();
            /*
            生成订单工序上料参数和加工参数
             */
            produceTaskBillRoutingMaterialService.create(taskBillRoutingProcessId, basicRoutingProcess);
            produceTaskBillRoutingParamService.create(taskBillRoutingProcessId, basicRoutingProcess);
        });
    }

    @Transactional
    @Override
    public void deleteByBillId(Long billId) {
        List<ProduceTaskBillRoutingProcess> processes = list(new LambdaQueryWrapper<ProduceTaskBillRoutingProcess>()
        .eq(ProduceTaskBillRoutingProcess::getTaskBillRoutingID,billId));
        if (CollectionUtils.isEmpty(processes)) {
            return;
        }
        processes.forEach(produceTaskBillRoutingProcess -> {
            Long processId = produceTaskBillRoutingProcess.getId();
            /*
            删除订单工序上料参数和加工参数
             */
            produceTaskBillRoutingMaterialService.deleteByProcessId(processId);
            produceTaskBillRoutingParamService.deleteByProcessId(processId);
            getBaseMapper().deleteById(produceTaskBillRoutingProcess.getId());
        });
    }

    @Override
    public List<ProduceTaskBillRoutingMaterial> getTaskBillRoutingMaterialListByTaskBillId(Long processId) {
        return produceTaskBillRoutingMaterialService.selectListByProcessId(processId);
    }

    @Override
    public List<ProduceTaskBillRoutingParam> getTaskBillRoutingParamListByTaskBillId(Long processId) {
        return produceTaskBillRoutingParamService.selectListByProcessId(processId);
    }

}
